<template>
  <el-drawer v-model="showDrawer"
             :title="title"
             :size="size"
             :close-on-click-modal="false"
             :destroy-on-close="destroyOnClose">
    <div class="formDrawer">
      <!-- 头部插槽区域 -->
      <div class="header">
        <slot name="header"></slot>
      </div>
      <div class="body">
        <slot></slot>
      </div>
      <div class="actions">
        <el-button type="primary" @click="submit" :loading="loading">{{ confirmText }}</el-button>
        <el-button type="default" @click="close">取消</el-button>
      </div>
    </div>
  </el-drawer>
</template>
<script setup>
import { ref } from "vue"

const showDrawer = ref(false)

const props = defineProps({
  title:String,
  size:{
    type:String,
    default:"45%"
  },
  destroyOnClose:{
    type:Boolean,
    default:false
  },
  confirmText:{
    type:String,
    default:"提交"
  },
})

const loading = ref(false)
const showLoading = ()=>loading.value = true
const hideLoading = ()=>loading.value = false

// 打开
const open = ()=> showDrawer.value = true

// 关闭
const close = ()=>showDrawer.value = false

// 提交
const emit = defineEmits(["submit"])
const submit = ()=> emit("submit")

// 向父组件暴露以下方法
defineExpose({
  open,
  close,
  showLoading,
  hideLoading
})

</script>
<style>
.formDrawer {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  /* 移除 position: relative */
}

.formDrawer .header {
  /* 移除 position: absolute */
  padding: 0 20px;
  margin-bottom: 10px;
  /* 添加固定高度 */
  height: 50px;
  /* 防止被压缩 */
  flex-shrink: 0;
}

.formDrawer .body {
  flex: 1;
  /* 移除 position: absolute 和相关定位属性 */
  overflow-y: auto;
  padding: 0 20px;
}

.formDrawer .actions {
  height: 50px;
  /* 移除 margin-top: auto */
  display: flex;
  align-items: center;
  /* 防止被压缩 */
  flex-shrink: 0;
  /* 可选：添加边框分隔 */
  border-top: 1px solid #eee;
}
</style>
